using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace KSSGenerator
{
    public partial class Generator : Form
    {
        public Generator()
        {
            InitializeComponent();
        }

        private DbmlToProjects ActiveGeneratorForm
        {
            get
            {
                return this.ActiveControl as DbmlToProjects;
            }
        }

        private void ShowNewGeneratorForm(object sender, EventArgs e)
        {
            DbmlToProjects frmGenerator = GetNewGeneratorForm();            
            frmGenerator.OpenConfigurationFile(Application.StartupPath + @"\schema.xml");
            frmGenerator.Show();
        }

        private void OpenConfigurationFile(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    DbmlToProjects frmGenerator = GetNewGeneratorForm();
                    frmGenerator.Show();
                    frmGenerator.OpenConfigurationFile(dlg.FileName);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private DbmlToProjects GetNewGeneratorForm()
        {
            DbmlToProjects frmGenerator = new DbmlToProjects();
            frmGenerator.MdiParent = this;

            return frmGenerator;
        }

        private void SaveConfiguration(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                if (ActiveGeneratorForm != null)
                {
                    ActiveGeneratorForm.SaveSetting(dlg.FileName);
                }
            }
        }

        private void Generate(object sender, EventArgs e)
        {
            if (ActiveGeneratorForm != null)
            {
                ActiveGeneratorForm.Generate();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}